package com.qf2207.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf2207.bo.DevicesBo;
import com.qf2207.common.LayuiPageVo;
import com.qf2207.mapper.DevicesMapper;
import com.qf2207.pojo.Devices;
import com.qf2207.service.DevicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
 * @Author LX
 * @Date 2022/7/26    14:57
 * @Version 3.3
 */
@Service("devicesService")
public class DevicesServiceImpl implements DevicesService {

    @Autowired
    private DevicesMapper devicesMapper;
    @Override
    public LayuiPageVo<DevicesBo> devicesPageData(Map<String, Object> params) {
        //mybatis 的分页插件
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page,limit);
        List<DevicesBo> devices = devicesMapper.selectByParams(params);

       //mybatis 的分页数据结构
        PageInfo<DevicesBo> devicesPageInfo = new PageInfo<>(devices);
        //把mybatis 的分页数据结构封装到 layui 的分页数据结构中
        LayuiPageVo<DevicesBo> devicesLayuiPageVo = new LayuiPageVo<>();
        devicesLayuiPageVo.setCode(0);
        devicesLayuiPageVo.setMsg("分页数据列表");
        devicesLayuiPageVo.setCount(devicesPageInfo.getTotal());
        devicesLayuiPageVo.setData(devicesPageInfo.getList());
        return devicesLayuiPageVo;
    }

    @Override
    public int addDevices(Devices devices) {
        return devicesMapper.addDevices(devices);
    }

    @Override
    public int deletedevices(Integer id) {
        return devicesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int devicesedit(Devices devices) {
        return devicesMapper.updateByPrimaryKeySelective(devices);
    }

    @Override
    public int deletedevicesByIds(String ids) {
        String arr[] = ids.split(",");
        return devicesMapper.deleteDevicesByIds(arr);
    }

    @Override
    public List<DevicesBo> devicesExcelData(Devices devices) {
        Devices devices1= new Devices();
        List<Devices> papers = devicesMapper.selectByExample(devices1);
//         要把 po 转为 bo
        List<DevicesBo> paperBos = papers.stream()
                .map((stu) -> {
                    DevicesBo paperBo = new DevicesBo();
                    paperBo.setId(stu.getId());
                    paperBo.setDeviceName(stu.getDeviceName());
                    paperBo.setDeviceLocation(stu.getDeviceLocation());
                    paperBo.setInstallDate(stu.getInstallDate());
                    paperBo.setWarrantyTime(stu.getWarrantyTime());
                    paperBo.setJob_name(stu.getJob_name());
                    paperBo.setDepart_name(stu.getDepart_name());
                    return paperBo;
                }).collect(Collectors.toList());

        return paperBos;
    }


}
